package testBase;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.openqa.selenium.support.ui.ExpectedConditions.alertIsPresent;

import java.util.HashSet;
import java.util.List;
import java.util.Random;

import objects.Movie;
import objects.MovieSet;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

import pages.HomePage;
import pages.MoviePage;
import pages.Page;

public class MovieBase extends TestBase {
	
	private MovieSet moviesModel;
	protected boolean moviesModelRebuild;
	private Random rnd = new Random();
	
	protected void prepare(int i) {
		logInByAdmin();
		synchronizeMovie();
		prepareMovies(i);
		
	}
	
	private void synchronizeMovie() {
		moviesModelRebuild = true;
		getMovies();
		moviesModelRebuild = false;
		
	}
	
	private MovieSet getMovies() {
			moviesModel = getUIMovies();
		return moviesModel;
	}
		

	public void prepareMovies(int i) {
		while (getMovies().size() < i) {
			Movie movie = app.getMovieDataGenerator().generateRandomMovie();
			
			createMovie(movie);
		}
	}
	
	public Movie createMovie(Movie movie) {
		Page.clickHome();
		Page.createRandomMovie(movie);
		return movie;
	
	}	
	
	public void movieModifacation(Movie movie) {
		Movie newMovie = app.getMovieDataGenerator().generateRandomMovie();
		Page.clickHome();
		Page.movieModify(movie, newMovie);
		moviesModel = moviesModel.without(movie).withAdded(newMovie);		
	}
	
	public void createRandomMovie() {
		Movie movie = app.getMovieDataGenerator().generateRandomMovie();
		createMovie(movie);
		moviesModel = moviesModel.withAdded(movie);
	}
	
	public Movie movieRemove(Movie movie) {
		HomePage.videoPageClick(movie.getId());
		MoviePage.deleteButton.click();
		wait.until(alertIsPresent()).accept();
		return movie;
	}
	
	public MovieSet getUIMovies() {
		HashSet<Movie> set = new HashSet<Movie>();
		HomePage.clickHome();
		List<WebElement> entries = findMovies();
		for (WebElement entry : entries) {
			Movie movie = getMovieDetails(entry);
			set.add(movie);
		}				
		MovieSet movies = new MovieSet(set);
		return movies;
	}
	
	private List<WebElement> findMovies() {
		WebElement results = HomePage.div.findElement(By.cssSelector("div[id='results']"));
		List<WebElement> entries = results.findElements(By.tagName("a"));	
		return entries;
	}
	
	private Movie getMovieDetails(WebElement entry) {
		String href = entry.getAttribute("href");
		String id = href.replace("http://localhost/php4dvd/?go=movie&id=", "");
		String title = entry.findElement(By.className("title")).getText();
		
		Movie movie = new Movie()
							.setTitle(title)
									.setId(id);
		return movie;
	}
	
	
	protected void testUIAndDBMovieListConformsModel() {
		MovieSet modelList = returnMoviesModel();
		MovieSet uiList = getUIMovies();
		assertThat(uiList,equalTo(modelList));
		System.out.println("uiList:" + uiList + "");
		System.out.println("modelList:" + modelList + "");
	}

	private MovieSet returnMoviesModel() {
	return moviesModel;
}

	public Movie getRandomMovie() {
		return (Movie) getSetElement(getMovies().getStoredMovies(), rnd.nextInt(getMovies().size()));
	}

}
